package com.axend.aerosense.room.ui.fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import android.text.TextUtils;
import android.view.View;

import com.axend.aerosense.base.bean.TimeZoneBean;
import com.axend.aerosense.base.utils.SystemUtil;
import com.axend.aerosense.room.entity.RoomNameBean;
import com.axend.aerosense.room.ui.activity.SaveModelActivity;
import com.axend.aerosense.room.viewmodel.RoomSetNameViewModel;
import com.blankj.utilcode.util.ToastUtils;
import com.axend.aerosense.base.fragment.MvvmBaseFragment;
import com.axend.aerosense.base.impl.IOperationDetailResult;
import com.axend.aerosense.base.livedatabus.LiveDatabus;
import com.axend.aerosense.common.base.IToolBarTextClickListence;
import com.axend.aerosense.common.config.Gobal;
import com.axend.aerosense.room.BR;
import com.axend.aerosense.room.R;
import com.axend.aerosense.room.databinding.RoomFragmentNameBinding;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link RoomNameFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class RoomNameFragment extends MvvmBaseFragment<RoomFragmentNameBinding, RoomSetNameViewModel> implements IToolBarTextClickListence {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ROOM_NAME = "OLD_NAME";
    private static final String ROOM_ID = "ROOM_ID";

    // TODO: Rename and change types of parameters
    private String oldName;
    private String roomUuid;

    public RoomNameFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param oldName Parameter 1.
     * @return A new instance of fragment RoomNameFragment.
     */
    // TODO: Rename and change types and number of parameter
    public static RoomNameFragment newInstance(String oldName, String roomUuid) {
        RoomNameFragment fragment = new RoomNameFragment();
        Bundle args = new Bundle();
        args.putString(ROOM_NAME, oldName);
        args.putString(ROOM_ID, roomUuid);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            oldName = getArguments().getString(ROOM_NAME);
            roomUuid = getArguments().getString(ROOM_ID);
        }
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if(!TextUtils.isEmpty(oldName)){
            viewDataBinding.roomName.setSelection(oldName.length());
        }

    }

    @Override
    public int getLayoutId() {
        return R.layout.room_fragment_name;
    }

    @Override
    public int getBindingVariable() {
        return BR.roomNameModel;
    }

    @Override
    protected RoomSetNameViewModel getViewModel() {
        RoomSetNameViewModel tmp = new ViewModelProvider(this).get(RoomSetNameViewModel.class);
        if(!TextUtils.isEmpty(oldName)){
            tmp.roomName.setValue(oldName);
        }
        return tmp;
    }

    @Override
    protected void onRetryBtnClick() {

    }

    @Override
    public void onSure() {
        String tmp = viewModel.roomName.getValue();
        if(TextUtils.isEmpty(tmp) && !TextUtils.isEmpty(roomUuid)){
            // 修改房间信息模式时,需要校验房间名称; 创建房间时, 则不需要,由房间类型名填充
            ToastUtils.showShort(R.string.room_name_input);
            return;
        }
        if(TextUtils.isEmpty(tmp)){
            ToastUtils.showShort(R.string.room_name_input);
            return;
        }
        String name = tmp.trim();

        if(!TextUtils.isEmpty(roomUuid)){
            // 修改房间信息模式
            TimeZoneBean timeZoneBean = SystemUtil.getTimeZone();
            RoomNameBean roomNameBean = new RoomNameBean(roomUuid, name, timeZoneBean.getRoomTimeZone(), timeZoneBean.getRoomTimeZoneArea());
            viewModel.setRoomName(getActivity(), roomNameBean, new IOperationDetailResult<String>(){

                @Override
                public void onSuccess(String s) {
                    viewModel.roomName.setValue(name);
                    LiveDatabus.getInstance().with(Gobal.EVENT_KEY.REFESH_ROOM, Integer.class).postValue(1);
                    LiveDatabus.getInstance().with(Gobal.EVENT_KEY.REFESH_ROOM_DETAIL, Integer.class).postValue(1);
                    finishSetting();

                }

                @Override
                public void onFail(int errCode, String message) {
                }
            });

        }else{
            finishSetting();
        }


    }

    private void finishSetting(){
        Intent intent = new Intent();
        intent.putExtra(SaveModelActivity.OPEN_MODE_ROOM_NAME, viewModel.roomName.getValue());
        getActivity().setResult(SaveModelActivity.REQUEST_CODE_SET_NAME, intent);
        getActivity().finish();
    }

    @Override
    public void onCancel() {
        getActivity().finish();
    }
}